home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS11.ADF / C / cpri.c < prev    next >
C/C++ Source or Header  |  1986-08-05  |  3KB  |  102 lines

  1. /*
  2.  * C P R I :    This allows you to muck around with the priorities of
  3.  *              running AmigaDOS processes.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include        <ctype.h>
  8. #include        <libraries/dos.h>
  9. #include        <libraries/dosextens.h>
  10. #include        <exec/tasks.h>
  11.  
  12. /* Externs */
  13. extern struct DosLibrary *DOSBase;      /* dos library base pointer */
  14. extern struct FileLock   *Output();     /* get output file handle */
  15. extern  short            SetTaskPri();  /* sets priority for specified task */
  16.  
  17. /* Globals */
  18. static struct FileLock   *OutLock;      /* used by WSTR define */
  19.  
  20. /* Defines */
  21.  
  22. /* Use AmigaDOS i/o to keep executable size down */
  23. #define WSTR(s)          (void)Write(OutLock, s, (long)strlen(s))
  24.  
  25. /* Casting conveniences */
  26. #define ROOTNODE        ((struct RootNode *)DOSBase->dl_Root)
  27.  
  28. main(argc, argv)
  29. int     argc;
  30. char    **argv;
  31. {
  32.         void            usage();
  33.         char            *pris = argv[1];
  34.         int             sign  = 1;
  35.         long            pri, atol();
  36.         struct Task     *tcb;
  37.         unsigned long   *tt, procnum;
  38.         struct   Task   *task;
  39.         UBYTE           *port;
  40.  
  41.         OutLock = Output();
  42.      
  43.         if (argc != 3) usage();
  44.  
  45.         /* Handle any leading + or - sign */
  46.         if (*pris == '+') {
  47.                 pris++;
  48.         } else if (*pris == '-') {
  49.                 pris++;
  50.                 sign = -1;
  51.         }
  52.      
  53.         /* What's left must be a digit string */
  54.         if (*pris == '\0' || (!isdigstr(pris)) ) usage();
  55.      
  56.         pri = atol(pris) * sign;                /* got the priority */
  57.  
  58.         /* Bound check the priority */
  59.         if (pri < -128 || pri > 127) {
  60.                 WSTR("Bad priority: range is -128 to 127\n");
  61.                 exit(20);
  62.         }
  63.      
  64.         /* The second parameter should be a CLI procnum */
  65.         if ( !isdigstr(argv[2]) ) usage();
  66.      
  67.         procnum = atol(argv[2]);
  68.         tt = (unsigned long *)(BADDR(ROOTNODE->rn_TaskArray));
  69.  
  70.         /* Validate with range check */
  71.         if (procnum > tt[0] || tt[procnum] == 0L) {
  72.                 WSTR("Invalid process number\n");
  73.                 exit(20);
  74.         }
  75.  
  76.         Forbid();       /* task mustn't go away while we fiddle with it */
  77.         port = (UBYTE *)tt[procnum];
  78.         tcb = (struct Task *)(port - sizeof(struct Task));
  79.  
  80.         /* Now we have a task, let's set its priority */
  81.         (void)SetTaskPri(tcb, pri);
  82.         Permit();
  83. }
  84.  
  85. void
  86. usage()
  87. {
  88.         WSTR("usage: cpri [+/-]priority CLI-process-number\n");
  89.         exit(20);
  90. }
  91.  
  92. isdigstr(s)
  93. char    *s;
  94. {
  95.         register char   *p;
  96.      
  97.         for(p =s ; *p != '\0'; p++)
  98.                 if (!isdigit(*p) )
  99.                         return 0;
  100.         return 1;
  101. }
  102.